// $Id: utility.js,v 1.2 2003/12/17 01:59:59 awilli07 Exp $
// THIS SCRIPT USED BY LIVEBANNER
// find anchor in document with given name
// returns reference to anchor object
function utility_findanchor(aname) {
    var links = document.links;
    var j;
    // find anchor (link) in doc corresonding to this aname
    for(j = 0; j < links.length; j++)
	if(links[j].name == aname)
	    return links[j];

    // no link element with aname - could be N4
    // N4 has hrefs in links[] and names in anchors[]
    var anc = document.anchors;
    for(j = 0; j < anc.length; j++)
        if(anc[j].name == aname)
	    return links[j];

    //alert("didn't find anchor named: " + aname);
    return null;
}

// select an image to change
// returns sel object:
//   sel.num  = index of new image to put in target
//   sel.targ = target position index for image
// returns select object containing num & targ properties (ints)
// count is the number of images to choose from
// current is an array of currently displayed images (index of image 0..count-1)
// prob is an optional array of probabilities (number >= 0)

function utility_selectimg(count, current, prob) {
    var av = new Array();  // available images
    var sel = new Object();
    var maxdisp = current.length;   // num of images displayed
    var probsum = 0;
    var i, j, p;

    sel.num = -1;
    sel.targ = Math.floor(Math.random() * maxdisp);

    for(i = 0; i < count; i++) av[i] = i;
    for(i = 0; i < maxdisp && i < count; i++) av[current[i]] = -1;

    for(i = 0; i < count; i++)
	if(av[i] >= 0) probsum += (prob == null ? 1 : prob[i]);
    p = probsum > 0 ? Math.floor(Math.random() * probsum) : -1;

    for(i = 0; i < count; i++)
        if(av[i] >= 0) {
            p -= prob == null ? 1 : prob[i];
            if(p < 0) {
                sel.num = i;
                break;
            }
        }
    return sel;
}
